home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Texteditors / Mg1b / Source / kbdmac.c < prev    next >
C/C++ Source or Header  |  1996-09-26  |  3KB  |  120 lines

  1. /*
  2.  *   This file defines everything we need to handle
  3.  *   nestable bindable macros.
  4.  */
  5. #include "def.h"
  6. /*
  7.  *   Currently we allow a maximum of 40 keys to be
  8.  *   bound to macros.
  9.  */
  10. #define MAXKMAC (40)
  11. struct macrobind {
  12.    KEY key, *macro ;
  13. } mbind[MAXKMAC] ;
  14. extern KEY *kbdmip, *kbdmop ;
  15. extern KEY *kbdnext ;
  16. extern KEY *lmalloc() ;
  17. /*
  18.  *   This macro takes the currently defined keyboard
  19.  *   macro and binds it to a key.  It copies the
  20.  *   macro higher in the array, and points things at
  21.  *   that copy for the next one.
  22.  */
  23. bindmacrotokey(f, n, c) {
  24.    register struct macrobind *mp ;
  25.    KEY *cur ;
  26.    KEY *p, *q ;
  27.    KEY bc ;
  28.    SYMBOL *sp ;
  29.  
  30.    if (kbdmip) {
  31.       ewprintf("Not now") ;
  32.       return(FALSE) ;
  33.    }
  34.    sp = symlookup("call-last-kbd-macro") ;
  35.    if (sp==NULL) {
  36.       ewprintf("Couldn't find call-last-kbd-macro") ;
  37.       return(FALSE) ;
  38.    }
  39.    cur = (KEY *)lmalloc(sizeof(KEY) * (long)(kbdnext-kbdm)) ;
  40.    if (cur==NULL) {
  41.       ewprintf("Out of macro space") ;
  42.       return(FALSE) ;
  43.    }
  44.    if (kbdmop==NULL)
  45.       ewprintf("Set key to macro: ") ;
  46.    bc = (int) getkey(0) ;
  47.    for (p=kbdm, q=cur; p<kbdnext;)
  48.       *q++ = *p++ ;
  49.    for (mp=mbind; mp<mbind+MAXKMAC; mp++) {
  50.       if (mp->key==bc) {
  51.          free(mp->macro) ;
  52.          mp->macro = cur ;
  53.       } else if (mp->key==0) {
  54.          mp->key = bc ;
  55.          mp->macro = cur ;
  56.          binding[(KEY) bc] = sp ;
  57.          return(TRUE) ;
  58.       }
  59.    }
  60.    ewprintf("Out of macro space") ;
  61.    return(FALSE) ;
  62. }
  63. /*
  64.  *   Here we find the macro associated with a particular
  65.  *   key, and bind everything.
  66.  */
  67. KEY *kbdloc(k) {
  68.    register struct macrobind *p ;
  69.  
  70.    if (k==KRANDOM || k==(KCTLX|'E'))
  71.       return(kbdm) ;
  72.    if (k==KMACCUR)
  73.       return(kbdnext + 10) ;
  74.    for (p=mbind; p<mbind+MAXKMAC; p++)
  75.       if (k==p->key)
  76.          return(p->macro) ;
  77.    return(NULL) ;
  78. }
  79. /*
  80.  *   This routine does some initialization which is best
  81.  *   done here.
  82.  */
  83. kbdminit() {
  84.    kbdm[0] =(KCTLX|')') ;
  85.    kbdnext = kbdm + 1 ;
  86. }
  87. /*
  88.  *   We even allow a stack to handle recursive macros.  We
  89.  *   don't let it get very deep, though.
  90.  */
  91. #define KSDEP (5)
  92. KEY *kbdstack[KSDEP] ;
  93. KEY **kbdsp = kbdstack ;
  94. /*
  95.  *   This routine pushes a position onto the stack.
  96.  */
  97. pushkbdm(k)
  98. KEY *k ;
  99. {
  100.    if (kbdsp >= kbdstack + KSDEP)
  101.       return(1) ;
  102.    *kbdsp++ = k ;
  103.    return(0) ;
  104. }
  105. /*
  106.  *   Here we pop one off.
  107.  */
  108. popkbdm() {
  109.    if (kbdsp > kbdstack)
  110.       kbdmop = (*--kbdsp) ;
  111.    else
  112.       kbdmop = NULL ;
  113. }
  114. /*
  115.  *   This forgets all of the macros.
  116.  */
  117. flushkbdm() {
  118.    kbdsp = kbdstack ;
  119. }
  120.